//-----------------------------------------------------------------------
// <copyright file="Object_AdessaDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class Object_AdessaDAOBase
    {
        #region Common methods
        public virtual Object_Adessa CreateObject_AdessaFromReader(IDataReader reader)
        {
            Object_Adessa item = new Object_Adessa();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("TGKM_ObjectId"))) item.TGKM_ObjectId = (long)reader["TGKM_ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateObject_AdessaFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateObject_Adessa methods
            
        public virtual void CreateObject_Adessa(Object_Adessa object_Adessa)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.StringFixedLength, object_Adessa.Name);
                database.AddOutParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                object_Adessa.TGKM_ObjectId = (long)database.GetParameterValue(dbCommand, "@TGKM_ObjectId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateObject_AdessaException, ex);
            }
        }

        #endregion

        #region UpdateObject_Adessa methods
        
        public virtual void UpdateObject_Adessa(Object_Adessa object_Adessa)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaUpdate");            
                
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, object_Adessa.TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@Name", DbType.StringFixedLength, object_Adessa.Name);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateObject_AdessaException, ex);
            }
        }
        
        #endregion

        #region DeleteObject_Adessa methods
        public virtual void DeleteObject_Adessa(long tGKM_ObjectId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaDelete");
                
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, tGKM_ObjectId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteObject_AdessaException, ex);
            }
        }

        #endregion

        #region GetObject_Adessa methods
        
        public virtual Object_Adessa GetObject_Adessa(long tGKM_ObjectId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaGet");
                
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, tGKM_ObjectId);
                
                Object_Adessa object_Adessa =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        object_Adessa = CreateObject_AdessaFromReader(reader);
                        reader.Close();
                    }
                }
                return object_Adessa;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObject_AdessaException, ex);
            }
        }
        
        #endregion

        #region GetObject_AdessaList methods
        public virtual Object_AdessaCollection GetObject_AdessaList(Object_AdessaColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                Object_AdessaCollection object_AdessaCollection = new Object_AdessaCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Object_Adessa object_Adessa = CreateObject_AdessaFromReader(reader);
                        object_AdessaCollection.Add(object_Adessa);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return object_AdessaCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObject_AdessaListException, ex);
            }
        }
        
        public virtual Object_AdessaCollection GetObject_AdessaList(Object_AdessaColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetObject_AdessaList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetObject_AdessaList methods
        public virtual Object_AdessaCollection GetObject_AdessaListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObject_AdessaGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                Object_AdessaCollection object_AdessaCollection = new Object_AdessaCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Object_Adessa object_Adessa = CreateObject_AdessaFromReader(reader);
                        object_AdessaCollection.Add(object_Adessa);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return object_AdessaCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObject_AdessaListException, ex);
            }
        }
        
        public virtual Object_AdessaCollection GetObject_AdessaListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetObject_AdessaListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}